package com.example.a22310.myapplication.ViewModel.Train;

import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.Toast;

import com.example.a22310.myapplication.Adapter.MvvmAdapter;
import com.example.a22310.myapplication.Beans.Train;
import com.example.a22310.myapplication.Listener.TrainListener;
import com.example.a22310.myapplication.Model.TrainModel;
import com.example.a22310.myapplication.R;

import java.util.List;

public class TrainBirdViewModel implements TrainListener<Train> {
    private FragmentBirdBinding fragmentTrainBinding;
    private FragmentActivity activity;

    public TrainBirdViewModel(FragmentActivity activity, FragmentBirdBinding fragmentTrainBinding){
        this.activity = activity;
        this.fragmentTrainBinding = fragmentTrainBinding;
        fragmentTrainBinding.setTrain(this);
        TrainModel trainModel=new TrainModel();
        trainModel.trainList(TrainBirdViewModel.this,1);
    }



    @Override
    public void onSuccess(List<Train> train) {
        if(!train.isEmpty()){
            fragmentTrainBinding.recyclerViewTrainlist.setLayoutManager(
                    new LinearLayoutManager(activity));
            MvvmAdapter adapter = new MvvmAdapter(activity, R.layout.cardview_trainlist,
                    train, BR.train);
            fragmentTrainBinding.recyclerViewTrainlist.setAdapter(adapter);

        }

    }

    @Override
    public void onFailure() {
        Toast.makeText(activity,"连接服务器失败",Toast.LENGTH_LONG).show();
    }
}

